/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-04-04     donghaomin    first version
 */

#include <rtthread.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#include <stdlib.h>
#include "usr_data.h"
#include <d_diag/diag.h>
#include "d_hardware.h"
#include <d_bluetooth/bluetooth.h>
#include "d_version.h"
#include "d_flash.h"

uint8_t aTxBuffer[]="uart2 send\n";
d_bule_revcmd d_revbuffer;

static char thread1_stack[1024];
static struct rt_thread thread1;

static char thread2_stack[1024];
static struct rt_thread thread2;
unsigned char KEY1;

void flash_function(void *arg) {
    rt_kprintf("flash_function\n");
    unsigned char testsram_get[22];
//    unsigned short testsram[10];
//    testsram[0] = 0x1234;
//    testsram[1] = 0x4321;
//    testsram[2] = 0x1234;
//    testsram[3] = 0x4321;
    FLASH_Init();
//    FLASH_Write(VERINFO_ADDR_BASE,(const unsigned short *)testsram,4);
    rt_kprintf("read byte %d\n",FLASH_Read(0x08008000+0x7C00,testsram_get,22));
    while(1) {
//        rt_kprintf("testsram_get[0] 0x %c ,testsram_get[1] 0x %c ,testsram_get[2] 0x %c ,testsram_get[3] 0x %c\n",testsram_get[0],testsram_get[1],testsram_get[2],testsram_get[3]);
        for(unsigned char count = 0;count<22;count++)
            rt_kprintf("%c",testsram_get[count]);
        rt_kprintf("\n");
        rt_thread_mdelay(1000);
    }

}
void Led_blink(void *arg)
{
    unsigned int count = 0;
    while(1)
    {
        rt_kprintf("led blink %d times\n",count);
        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);  // LED引脚翻转，造成LED闪烁，只要闪烁系统就正常运行，没有卡死
        rt_thread_mdelay(1000);
        count++;
    }
}

int main(void)
{
//    unsigned char data[10] = {0};
    errornum ret = DIAG_OK;
    d_bule_temp * d_TempHandle = NULL;
    int len = 0;
    rt_thread_init(&thread1,"flash", flash_function, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), 24,5);
    rt_thread_startup(&thread1);
    rt_thread_init(&thread2,"led", Led_blink, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), 24,5);
    rt_thread_startup(&thread2);

    dataInit();
    Dong_MX_GPIO_Init();
    Dong_MX_USART2_UART_Init();
    d_TempHandle = getTempHandle();
//    d_TempHandle->blueTooth_state = bluetooth_AT_OK();

    while (Dong_TURE)
    {
        if (KEY1 == 0x01) {
            // 判断数据有没有超出最大范围
            if(d_TempHandle->usedDataCount < DATA_SUM) {
                // 保存温度数据
                d_TempHandle->usedDataCount ++;
                d_TempHandle->data->id = d_TempHandle->usedDataCount ++;
                d_TempHandle->data->value = (float)(rand()%100);
                d_TempHandle->data->state = Dong_TURE;
            } else {
                rt_kprintf("save data out of range\n");
            }
            KEY1 = 0x00;
        }
        if(USART_RX_STA&0x8000)
            {
                len=USART_RX_STA&0x3fff;
                memcpy(&d_revbuffer,USART_RX_BUF,len);
                memset(USART_RX_BUF,0,sizeof(USART_RX_BUF));
                USART_RX_STA=0;
                // 判断接收的数据
                if (d_revbuffer.really == 0x01) {
                    // 将接收到的数据进行分析和处理
                    ret = bule_revdata_Diag(&d_revbuffer);
                    rt_kprintf("recive data diag ret = %d\n",ret);
                    memset(&d_revbuffer,0,sizeof(d_revbuffer));
                    d_revbuffer.really = Dong_FALSE;
                }
            }

        rt_thread_mdelay(50);    // 延时1000ms
    }

    return RT_EOK;
}

